package cn.ransj.animation.util;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore.Images.Media;
import android.util.Log;

/**
 * 
 * @author android
 * 
 */
public class Utils {
	private static final String[] IMG_PROS = { 
		Media.DISPLAY_NAME,//
			Media.LATITUDE, //
			Media.LONGITUDE, //
			Media._ID };//

	public static Cursor getImagesCursor(Activity context) {
		Cursor cursor = context.getContentResolver().query(
				Media.EXTERNAL_CONTENT_URI, IMG_PROS, null, null, null);
		cursor.moveToFirst();
		return cursor;
	}

	public static Bitmap getSmallImg(ContentResolver resolver, Uri uri,
			int outWidth, int outHeight) {
		try {
			// Decode image size
			BitmapFactory.Options o = new BitmapFactory.Options();
			o.inJustDecodeBounds = true;
			BitmapFactory.decodeStream(resolver.openInputStream(uri), null, o);
			// Find the correct scale value. It should be the power of 2.
			int width_tmp = o.outWidth, height_tmp = o.outHeight;
			System.out.println(width_tmp + "  " + height_tmp);
			Log.w("===", (width_tmp + "  " + height_tmp));

			int scale = 1;
			while (true) {
				if (width_tmp / 2 < outWidth && height_tmp / 2 < outHeight)
					break;
				width_tmp /= 2;
				height_tmp /= 2;
				scale++;

				Log.w("===", scale + "''" + width_tmp + "  " + height_tmp);
			}
			// Decode with inSampleSize
			BitmapFactory.Options o2 = new BitmapFactory.Options();
			o2.inSampleSize = scale;
			return BitmapFactory.decodeStream(resolver.openInputStream(uri),
					null, o2);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
